from django.contrib import admin
from celery_tasks.static_html.tasks import generate_static_list_search_html, generate_static_sku_detail_html
from goods import models


class GoodsAdmin(admin.ModelAdmin):
    list_display = ["id","name"]

    # 使用定时任务生成新的静态页面
    # 借助admin 保存的时候生成新的静态页面的
    def save_model(self, request, obj, form, change):
        """
        更新保存数据
        :param request:
        :param obj:
        :param form:
        :param change:
        :return:
        """
        # 保存到数据库
        obj.save()
        generate_static_list_search_html.delay()

    def delete_model(self, request, obj):
        """
        删除数据
        :param request:
        :param obj:
        :return:
        """
        obj.delete()
        generate_static_list_search_html.delay()


class SKUAdmin(admin.ModelAdmin):
    """
    商品详情admin模型
    """
    def save_model(self, request, obj, form, change):
        obj.save()
        generate_static_sku_detail_html.delay(obj.id)



admin.site.register(models.Goods,GoodsAdmin)
admin.site.register(models.GoodsCategory)
admin.site.register(models.GoodsChannel)
admin.site.register(models.Brand)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU,SKUAdmin)
admin.site.register(models.SKUSpecification)
admin.site.register(models.SKUImage)

